home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / sphigs / sph_srgp.lha / srgp / src / srgp_input.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-09  |  12.7 KB  |  563 lines

  1. #include "HEADERS.h"
  2. #include "srgplocal.h"
  3. #ifdef UNIX
  4. #   include <sys/types.h>
  5. #   include <sys/time.h>
  6. #endif
  7.  
  8.  
  9. /** DEVICE INDEPENDENCE
  10. This file contains functions that are device-independent,
  11.    except for the "sleeping" arrangements for SRGP_waitEvent.
  12. Refer to inputraw.c and echo.c for the low-level "drivers" 
  13.    for each type of implementation.
  14.  
  15. **/
  16.  
  17.  
  18. static void
  19. ComputeTimestamp (srgp_timestamp *ts)
  20. {
  21.    Time tdiff =  srgpx__cur_time - srgpx__starttime;
  22.  
  23.    ts->seconds = tdiff / rawgranularity;
  24.    ts->ticks = (tdiff % rawgranularity) / ((double)rawgranularity) * 60;
  25. }
  26.  
  27.  
  28.  
  29. /** SRGP __ initInputModule **/
  30. void
  31. SRGP__initInputModule ()
  32. {
  33.    /* DEFAULT KEYBOARD ECHO POSITION IS MIDDLE OF SCREEN WINDOW. */
  34.    srgp__cur_keyboard_echo_origin = 
  35.       SRGP_defPoint(srgp__canvasTable[0].max_xcoord >> 1,
  36.             srgp__canvasTable[0].max_ycoord >> 1);
  37.    srgp__cur_locator_echo_anchor = srgp__cur_keyboard_echo_origin;
  38.  
  39.    /* DEFAULT KEYBOARD ATTRIBUTES */
  40.    srgp__cur_keyboard_echo_font = 0;
  41.    srgp__cur_keyboard_echo_color = SRGP_BLACK;
  42.  
  43.    SRGP__disableLocatorCursorEcho();
  44.  
  45.    /* INITIALIZE CURSOR TABLE. */
  46.    PUSH_TRACE;
  47.    /* SRGP_loadCursor (0, XC_arrow);    "device" dependent!!! */
  48.    POP_TRACE;
  49.    srgp__cur_cursor = 0;
  50.  
  51.    /* INITIALIZE ACTIVITY INFORMATION. */
  52.    srgp__cur_mode[LOCATOR]=srgp__cur_mode[KEYBOARD]=INACTIVE;
  53.    srgp__cur_locator_echo_type = CURSOR;
  54.    srgp__cur_keyboard_processing_mode = EDIT;
  55.  
  56.    /* INITIALIZE MEASURES AND MASKS */
  57.    srgp__cur_locator_measure.button_chord[LEFT_BUTTON] = UP;
  58.    srgp__cur_locator_measure.button_chord[MIDDLE_BUTTON] = UP;
  59.    srgp__cur_locator_measure.button_chord[RIGHT_BUTTON] = UP;
  60.    srgp__cur_locator_measure.position = srgp__cur_locator_echo_anchor;
  61.  
  62.    srgp__cur_keyboard_measure.buffer = malloc(MAX_STRING_SIZE+1);
  63.    srgp__cur_keyboard_measure.buffer_length = MAX_STRING_SIZE+1;
  64.    srgp__cur_keyboard_measure.buffer[0] = '\0';
  65.    srgp__get_keyboard_measure.buffer = malloc(MAX_STRING_SIZE+1);
  66.    srgp__get_keyboard_measure.buffer_length = MAX_STRING_SIZE+1;
  67.    srgp__get_keyboard_measure.buffer[0] = '\0';
  68.  
  69.    srgp__cur_locator_button_mask = LEFT_BUTTON_MASK;
  70.  
  71.    SRGP__initInputDrivers ();
  72.    SRGP__initEchoModule ();
  73. }
  74.    
  75.  
  76.  
  77.  
  78. /** SRGP set input mode
  79. The device is first deactivated, then its cur_mode status is updated,
  80. then it is activated
  81. **/
  82. void
  83. SRGP_setInputMode (inputDevice device, inputMode mode)
  84. {
  85.    DEBUG_AIDS{
  86.       SRGP_trace (SRGP_logStream, "SRGP_setInputMode  %d into %d\n", 
  87.           device, mode);
  88.       srgp_check_system_state();
  89.       srgp_check_device (device);
  90.       srgp_check_mode (mode);
  91.       LeaveIfNonFatalErr();
  92.    }
  93.  
  94.    if (mode == srgp__cur_mode[device])
  95.       /* NOTHING IS TO BE DONE. */
  96.       return;
  97.  
  98.    if (mode == INACTIVE) {
  99.       /* WE ARE BRINGING AN ACTIVE DEVICE TO INACTIVITY. */
  100.       SRGP__deactivateDevice (device);
  101.       srgp__cur_mode[device] = INACTIVE;
  102.    }
  103.  
  104.    else {
  105.       if ((device==LOCATOR)&&(srgp__cur_mode[LOCATOR]==INACTIVE))
  106.      SRGP__updateRawCursorPosition();
  107.       srgp__cur_mode[device] = mode;
  108.       SRGP__activateDevice (device);
  109.    }
  110. }
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117. /** SRGP wait event
  118. The event in the queue which satisfies the wait is removed
  119. and placed at the tail of the free-list.  It is there that
  120. the "get" routines look.
  121.  
  122. Returns the identifier of the device which issued the event.
  123.    (NO_DEVICE if none).
  124. **/
  125.  
  126. inputDevice
  127. SRGP_waitEvent (maximum_wait_time)
  128. int maximum_wait_time;
  129. {
  130. #ifdef X11
  131.    struct timeval tp, tzp, expiretime, timeout, *timeout_ptr;
  132.    int fd;
  133.    fd_set readfds;
  134. #endif
  135. #ifdef THINK_C   
  136.    Time curtime, expiretime=0;
  137. #endif
  138.    int return_value;
  139.    boolean do_continue_wait, do_continue_search;
  140.    boolean forever = (maximum_wait_time < 0);
  141.  
  142.  
  143.    DEBUG_AIDS{
  144.       srgp_check_system_state();
  145.       LeaveIfNonFatalErr();
  146.    }
  147.  
  148.  
  149. #ifdef UNIX
  150. #define MILLION 1000000
  151.    FD_ZERO (&readfds);
  152.    FD_SET (fd=ConnectionNumber(srgpx__display), &readfds);
  153.  
  154.    /* INITIALIZE (if necessary) TIMING INFORMATION */
  155.    if (maximum_wait_time > 0) {
  156.       long maxwait_sec, maxwait_ticks;
  157.       gettimeofday (&tp,&tzp);
  158.       maxwait_sec = maximum_wait_time / 60;
  159.       maxwait_ticks = maximum_wait_time % 60;
  160.       expiretime.tv_usec = tp.tv_usec + (maxwait_ticks*MILLION/60);
  161.       expiretime.tv_sec = tp.tv_sec + maxwait_sec + 
  162.                       (expiretime.tv_usec / MILLION);
  163.       expiretime.tv_usec %= MILLION;
  164.    }
  165. #endif
  166.  
  167. #ifdef THINK_C
  168.    expiretime = TickCount() + maximum_wait_time;
  169. #endif
  170.  
  171.    do_continue_wait = TRUE;
  172.  
  173.    return_value = NO_DEVICE;  /* timeout is what we assume will happen */
  174.  
  175.  
  176.    /**** LOOP *****/
  177.    do {
  178.       return_value = SRGP__handleRawEvents (TRUE,forever);
  179.  
  180.       if ((return_value != NO_DEVICE) || (maximum_wait_time == 0))
  181.      do_continue_wait = FALSE;
  182.  
  183. #ifdef UNIX
  184.       /* Otherwise, perform a wait state */
  185.       else {
  186.      if (!forever) {
  187.         gettimeofday (&tp,&tzp);
  188.         /* Perform subtraction: expiretime - tp */
  189.         if (expiretime.tv_usec < tp.tv_usec) {
  190.            /* perform a borrow */
  191.            expiretime.tv_sec--;
  192.            expiretime.tv_usec += 1000000;
  193.         }
  194.         timeout_ptr = &timeout;
  195.         timeout.tv_usec = expiretime.tv_usec - tp.tv_usec;
  196.         timeout.tv_sec  = expiretime.tv_sec  - tp.tv_sec;
  197.         if ((timeout.tv_sec < 0) || (timeout.tv_usec < 0))
  198.            /* Whoops!  We've already expired! */
  199.            do_continue_wait = FALSE;
  200.      }
  201.      else /* (maximum_wait_time is negative representing infinity) */
  202.         timeout_ptr = NULL;
  203.  
  204.      if (do_continue_wait)
  205.         if (XPending(srgpx__display) == 0) {
  206.            if (select (fd+1, &readfds, NULL, NULL, timeout_ptr))
  207.           /* An event occurred before the timeout! */
  208.           ;  /* so do nothing */
  209.            else 
  210.           do_continue_wait = FALSE;
  211.         }
  212.       }
  213. #endif
  214.  
  215. #ifdef THINK_C
  216.       else
  217.          if (maximum_wait_time > 0) {
  218.             do_continue_wait =  (TickCount() < expiretime);
  219.          }
  220. #endif
  221.  
  222.    }
  223.    while (do_continue_wait);
  224.  
  225.    srgp__device_at_head_of_queue = return_value;
  226.  
  227.    return return_value;
  228. }
  229.  
  230.  
  231.  
  232. void
  233. SRGP_getLocator (srgp__locator_measure *measure)
  234. {
  235.    DEBUG_AIDS{
  236.       srgp_check_system_state();
  237.       srgp_check_event_type (LOCATOR);
  238.       LeaveIfNonFatalErr();
  239.    }
  240.  
  241.    /* this assignment statement is very risky!  God help me! */
  242.    *measure = *((srgp__locator_measure*)(&srgp__get_locator_measure));
  243. }
  244.  
  245.  
  246. void
  247. SRGP_getDeluxeLocator (srgp__deluxe_locator_measure *measure)
  248. {
  249.    DEBUG_AIDS{
  250.       srgp_check_system_state();
  251.       srgp_check_event_type (LOCATOR);
  252.       LeaveIfNonFatalErr();
  253.    }
  254.  
  255.    *measure = srgp__get_locator_measure;
  256.    ComputeTimestamp (&(measure->timestamp));
  257. }
  258.  
  259.  
  260.  
  261. void
  262. SRGP_getKeyboard (char *measure, int bufsize)
  263. {
  264.    DEBUG_AIDS{
  265.       srgp_check_system_state();
  266.       srgp_check_event_type (KEYBOARD);
  267.       LeaveIfNonFatalErr();
  268.    }
  269.    strncpy (measure, srgp__get_keyboard_measure.buffer, bufsize-1);
  270.    *(measure+bufsize-1) = '\0';
  271. }
  272.  
  273.  
  274. void
  275. SRGP_getDeluxeKeyboard (srgp__deluxe_keyboard_measure *measure)
  276. {
  277.    DEBUG_AIDS{
  278.       srgp_check_system_state();
  279.       srgp_check_event_type (KEYBOARD);
  280.       LeaveIfNonFatalErr();
  281.    }
  282.    strncpy (measure->buffer, srgp__get_keyboard_measure.buffer, 
  283.         measure->buffer_length-1);
  284.    *(measure->buffer+measure->buffer_length-1) = '\0';
  285.    bcopy (srgp__get_keyboard_measure.modifier_chord,
  286.       measure->modifier_chord,
  287.       sizeof(measure->modifier_chord));
  288.    measure->position = srgp__get_keyboard_measure.position;
  289.    
  290.    ComputeTimestamp (&(measure->timestamp));
  291. }
  292.  
  293.  
  294.  
  295.  
  296. /** MEASURE SETTING **/
  297.  
  298. void
  299. SRGP_setLocatorMeasure (point position)
  300. {
  301.    DEBUG_AIDS{
  302.       SRGP_trace (SRGP_logStream, "SRGP_setLocatorMeasure %d,%d\n", 
  303.           position.x, position.y);
  304.       srgp_check_system_state();
  305.       LeaveIfNonFatalErr();
  306.    }
  307.  
  308.    SRGP__handleRawEvents (FALSE,FALSE);
  309.  
  310.    srgp__cur_locator_measure.position = position;
  311.  
  312.    SRGP__updateRawCursorPosition ();
  313. }
  314.  
  315.  
  316. /*!*/
  317. void
  318. SRGP_setKeyboardMeasure (str)
  319. char *str;
  320. {
  321.    DEBUG_AIDS{
  322.       SRGP_trace (SRGP_logStream, "SRGP_setKeyboardMeasure %s\n", str);
  323.       srgp_check_system_state();
  324.       LeaveIfNonFatalErr();
  325.    }
  326.    SRGP__handleRawEvents (FALSE,FALSE);
  327.  
  328.    strncpy (srgp__cur_keyboard_measure.buffer, str, MAX_STRING_SIZE);
  329.    *(srgp__cur_keyboard_measure.buffer+MAX_STRING_SIZE) = '\0';
  330.    srgp__cur_keyboard_measure_length = strlen(srgp__cur_keyboard_measure.buffer);
  331.  
  332.    SRGP__updateKeyboardEcho ();
  333. }
  334.  
  335.  
  336.  
  337.  
  338. /** ATTRIBUTES
  339. The routines allow the application to control the echoing
  340. associated with the keyboard and the locator devices.
  341. **/
  342.  
  343. /*!*/
  344.  
  345. void
  346. SRGP_setLocatorEchoType (echoType value)
  347. {
  348.    DEBUG_AIDS{
  349.       SRGP_trace (SRGP_logStream, "SRGP_setLocatorEchoType %d\n", value);
  350.       srgp_check_system_state();
  351.       srgp_check_locator_echo_type(value);
  352.       LeaveIfNonFatalErr();
  353.    }
  354.  
  355.    SRGP__handleRawEvents (FALSE,FALSE);
  356.  
  357.    SRGP__disableLocatorRubberEcho();
  358.    SRGP__disableLocatorCursorEcho();
  359.  
  360.    srgp__cur_locator_echo_type = value;
  361.  
  362.    SRGP__enableLocatorRubberEcho();
  363.    SRGP__enableLocatorCursorEcho();
  364.  
  365.    SRGP__updateInputSelectionMask();
  366. }
  367.  
  368.    
  369. /*!*/
  370.  
  371. void
  372. SRGP_setLocatorEchoCursorShape (int id)
  373. {
  374.    DEBUG_AIDS{
  375.       SRGP_trace (SRGP_logStream, "SRGP_setLocatorEchoCursorShape %d\n", id);
  376.       srgp_check_system_state();
  377.       LeaveIfNonFatalErr();
  378.    }
  379.    SRGP__handleRawEvents (FALSE,FALSE);
  380.  
  381.    srgp__cur_cursor = id;
  382.  
  383.    SRGP__updateLocatorCursorShape ();
  384. }
  385.  
  386.  
  387. /*!*/
  388.  
  389. void
  390. SRGP_setLocatorEchoRubberAnchor (point position)
  391. {
  392.    DEBUG_AIDS{
  393.       SRGP_trace (SRGP_logStream, "SRGP_setLocatorEchoRubberAnchor %d,%d\n",
  394.           position.x, position.y);
  395.       srgp_check_system_state();
  396.       LeaveIfNonFatalErr();
  397.    }
  398.  
  399.    SRGP__handleRawEvents (FALSE,FALSE);
  400.  
  401.    srgp__cur_locator_echo_anchor = position;
  402.  
  403.    SRGP__updateLocatorRubberAnchor ();
  404. }
  405.  
  406.  
  407. /*!*/
  408.  
  409.  
  410. void
  411. SRGP_setLocatorButtonMask (int value)
  412. {
  413.    DEBUG_AIDS{
  414.       SRGP_trace (SRGP_logStream, "SRGP_setLocatorButtonMask %d\n", value);
  415.    }
  416.  
  417.    srgp__cur_locator_button_mask = value;
  418. }
  419.  
  420.  
  421. /*!*/
  422.  
  423.  
  424. void
  425. SRGP_setKeyboardProcessingMode (keyboardMode value)
  426. {
  427.    DEBUG_AIDS{
  428.       SRGP_trace (SRGP_logStream, "SRGP_setKeyboardProcessingMode %d\n", 
  429.           value);
  430.       srgp_check_system_state();
  431.       LeaveIfNonFatalErr();
  432.    }
  433.    SRGP__handleRawEvents (FALSE,FALSE);
  434.  
  435.    if (srgp__cur_mode[KEYBOARD] != INACTIVE)
  436.       SRGP__deactivateDevice (KEYBOARD);
  437.  
  438.    srgp__cur_keyboard_processing_mode = value;
  439.  
  440.    if (srgp__cur_mode[KEYBOARD] != INACTIVE)
  441.       SRGP__activateDevice (KEYBOARD);
  442. }
  443.  
  444.  
  445. /*!*/
  446.  
  447.  
  448. void
  449. SRGP_setKeyboardEchoColor (int value)
  450. {
  451.    DEBUG_AIDS{
  452.       SRGP_trace (SRGP_logStream, "SRGP_setKeyboardEchoColor %d\n", value);
  453.       srgp_check_system_state();
  454.       LeaveIfNonFatalErr();
  455.    }
  456.    SRGP__handleRawEvents (FALSE,FALSE);
  457.  
  458.    srgp__cur_keyboard_echo_color = value;
  459.    SRGP__updateKeyboardEchoAttributes();
  460. }
  461.  
  462.  
  463. /*!*/
  464.  
  465.  
  466. void
  467. SRGP_setKeyboardEchoOrigin (point position)
  468. {
  469.    DEBUG_AIDS{
  470.       SRGP_trace (SRGP_logStream, "SRGP_setKeyboardEchoOrigin %d,%d\n", 
  471.           position.x, position.y);
  472.       srgp_check_system_state();
  473.       LeaveIfNonFatalErr();
  474.    }
  475.    SRGP__handleRawEvents (FALSE,FALSE);
  476.  
  477.    srgp__cur_keyboard_echo_origin = position;
  478.    SRGP__updateKeyboardEchoAttributes();
  479. }
  480.  
  481.  
  482. /*!*/
  483.  
  484.  
  485. void
  486. SRGP_setKeyboardEchoFont (int fontindex)
  487. {
  488.    DEBUG_AIDS{
  489.       SRGP_trace (SRGP_logStream, "SRGP_setKeyboardEchoFont %d\n", fontindex);
  490.       srgp_check_system_state();
  491.       LeaveIfNonFatalErr();
  492.    }
  493.    SRGP__handleRawEvents (FALSE,FALSE);
  494.  
  495.    srgp__cur_keyboard_echo_font = fontindex;
  496.    SRGP__updateKeyboardEchoAttributes ();
  497. }
  498.  
  499.  
  500.  
  501.  
  502. /** SAMPLERS **/
  503.  
  504. void
  505. SRGP_sampleLocator (srgp__locator_measure *measure)
  506. {
  507.    DEBUG_AIDS{
  508.       srgp_check_system_state();
  509.       LeaveIfNonFatalErr();
  510.    }
  511.    SRGP__handleRawEvents (FALSE,FALSE);
  512.    if (srgp__dirty_location) 
  513.       SRGP__updateLocationKnowledge();
  514.    *measure = *((srgp__locator_measure*)(&srgp__cur_locator_measure));
  515. }
  516.  
  517.  
  518. void
  519. SRGP_sampleDeluxeLocator (srgp__deluxe_locator_measure *measure)
  520. {
  521.    DEBUG_AIDS{
  522.       srgp_check_system_state();
  523.       LeaveIfNonFatalErr();
  524.    }
  525.    SRGP__handleRawEvents (FALSE,FALSE);
  526.    if (srgp__dirty_location) 
  527.       SRGP__updateLocationKnowledge();
  528.    *measure = srgp__cur_locator_measure;
  529.    ComputeTimestamp (&(measure->timestamp));
  530. }
  531.  
  532. /*!*/
  533.  
  534. void
  535. SRGP_sampleKeyboard (char *measure, int bufsize)
  536. {
  537.    DEBUG_AIDS{
  538.       srgp_check_system_state();
  539.       LeaveIfNonFatalErr();
  540.    }
  541.    SRGP__handleRawEvents (FALSE,FALSE);
  542.    strncpy (measure, srgp__cur_keyboard_measure.buffer, bufsize-1);
  543.    *(measure+bufsize-1) = '\0';
  544. }
  545.  
  546. void
  547. SRGP_sampleDeluxeKeyboard (srgp__deluxe_keyboard_measure *measure)
  548. {
  549.    DEBUG_AIDS{
  550.       srgp_check_system_state();
  551.       LeaveIfNonFatalErr();
  552.    }
  553.    SRGP__handleRawEvents (FALSE,FALSE);
  554.    strncpy (measure->buffer, srgp__cur_keyboard_measure.buffer, 
  555.         measure->buffer_length-1);
  556.    *(measure->buffer+measure->buffer_length-1) = '\0';
  557.    bcopy (srgp__cur_keyboard_measure.modifier_chord,
  558.       measure->modifier_chord,
  559.       sizeof(measure->modifier_chord));
  560.    measure->position = srgp__get_keyboard_measure.position;
  561.    ComputeTimestamp (&(measure->timestamp));
  562. }
  563.